package com.swak.cleaner;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * 与 key 有关系
 * 
 * @author 618lf
 */
@SuppressWarnings("rawtypes")
public class Weakmap {
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		String a = new String("a");
		String b = new String("b");

		Map weakmap = new WeakHashMap();
		Map map = new HashMap();
		map.put(a, "aaa");
		map.put(b, "bbb");

		weakmap.put(a, "aaa");
		weakmap.put(b, "bbb");

		map.remove(a);

		a = null;
		b = null;

		System.gc();
		Iterator i = map.entrySet().iterator();
		while (i.hasNext()) {
			Map.Entry en = (Map.Entry) i.next();
			System.out.println("map:" + en.getKey() + ":" + en.getValue());
		}

		Iterator j = weakmap.entrySet().iterator();
		while (j.hasNext()) {
			Map.Entry en = (Map.Entry) j.next();
			System.out.println("weakmap:" + en.getKey() + ":" + en.getValue());
		}
	}
}
